<html>
<%@ include file="../../common/taglibs.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title><fmt:message key="format.message.system" /></title>

<script type="text/javascript" >
//游戏管理页面
Ext.namespace('azure', 'azure.common');
azure.common.agentListed = function() {
	var cm_agented;
	var grid_agented, store_agented;
	var mainpanel;
	var toolbar_res;
	var addAgentForm,editAgentForm;
	var addAgentWin,editAgentWin;
	var rowActions;
	
	// 創建ColumnModel
	function createCM() {
		
		cm_agented = new Ext.grid.ColumnModel([
		{header : '<fmt:message key="format.agent.loginName"/>',dataIndex : 'uname'},
		{header : '<fmt:message key="format.agent.userName"/>',dataIndex : 'trueName'},
		{header : '<fmt:message key="format.agent.phone"/>',dataIndex : 'tel'}, 
		{header : '<fmt:message key="format.content.Status"/>',dataIndex : 'status'},
		{header : '<fmt:message key="format.content.Operation"/>',width:280,dataIndex : 'status',renderer : function(val, p, record) {
			var str;
			if(val == '冻结'){
				str = '<a href="#" onclick="javascript:statusUpdate(\'正常\');"}><fmt:message key="format.content.open" /></a>'
			}else{
				str = '<a href="#"} onclick="javascript:statusUpdate(\'冻结\');"><fmt:message key="format.content.close" /></a>'
			}
			return '<b><a href="#" onclick="javascript:editagentListed();"}><fmt:message key="format.content.update" /></a>&nbsp; '+
			'<a href="#" onclick="javascript:delAgentList();"}><fmt:message key="format.content.delete" /></a>&nbsp; '+
			'<a href="#" onclick="javascript:agenListedLog();"}><fmt:message key="format.agent.historyOperation" /></a>&nbsp; '+str+'</b>';
		}}
		]);


	}

	function createStore() {
		store_agented = new Ext.data.Store({
			id:'agentedStore',
			proxy : new Ext.data.HttpProxy({
				url : 'Agent_findSubAgents.action'
			}),
			reader : new Ext.data.JsonReader({
				root : 'pager.data',
				totalProperty : 'pager.totalSize'
			}, [
		{
			name : 'uname',
			mapping : 'uname'
		}, {
			name : 'trueName',
			mapping : 'trueName'
		}, {
			name : 'tel',
			mapping : 'tel'
		}, {
			name : 'status',
			mapping : 'status'
		}, {
			name : 'email',
			mapping : 'email'
		}, {
			name : 'uno',
			mapping : 'uno'
		}])
		});
	}

	function createToolbar() {
		toolbar_res = new Ext.Toolbar([{
			text : '<fmt:message key="format.message.add"/>',
			handler : addNew
		}]);
	}
	function createGrid() {
		createCM();
		createStore();
		createToolbar();

		grid_agented = new Ext.grid.GridPanel({
			cm : cm_agented,
			store : store_agented,
			height :490,			
			bbar : new Ext.PagingToolbar({
				grid : grid_agented,
				store : store_agented,
				pageSize : 15,
				displayInfo : true,
				displayMsg : '<fmt:message key="format.paging.displayMsg"/>',
				emptyMsg : '<fmt:message key="format.paging.emptyMsg"/>'
			})
		});
		//grid_agented.render();
		store_agented.load({params:{
			start:0,
			limit:15,
			agentType:0
		}});

	}

	function createUI() {
		createGrid();
		createForm();
		mainpanel = new Ext.Panel({
			frame : false,
			renderTo : 'agentedDiv',
			items : [toolbar_res, grid_agented]
		});
	}

	function createForm() {
		
		addAgentForm = new Ext.form.FormPanel({
			id:'addatention',
			labelWidth : 65,
			frame : true,
			url:'Agent_create.action',
			labelSeparator : ':',
			labelAlign : 'left',
			defaultType : 'textfield',
			items : [new Ext.form.Hidden({
				name : 'agent.agentType',
				value:0
			}),new Ext.form.Hidden({
				name : 'agent.parentAgent.uno',
				value:'${user.uno}'
			}),new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.agent.loginName"/>',
				width : 220,
				name : 'agent.uname'
			}),new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.message.pwd"/>',
				width : 220,
				name : 'agent.upwd'
			}),new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.agent.userName"/>',
				width : 220,
				name : 'agent.trueName'
			}),new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.agent.phone"/>',
				width : 220,
				name : 'agent.contactInfo.tel'
			}),new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.agent.email"/>',
				width : 220,
				name : 'agent.contactInfo.email'
			})
			]
		});
		
		editAgentForm = new Ext.form.FormPanel({
			labelWidth : 65,
			frame : true,
			url:'Agent_updateAgent.action',
			labelSeparator : ':',
			labelAlign : 'right',
			defaultType : 'textfield',
			items : [new Ext.form.Hidden({
				id:'uno',
				name : 'agent.uno'
			}),new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.agent.loginName"/>',
				id:'uname',
				width : 220,
				name : 'agent.uname'
			}),new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.message.pwd"/>',
				id:'upwd',
				width : 220,
				name : 'agent.upwd'
			}),new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.agent.userName"/>',
				id:'trueName',
				width : 220,
				name : 'agent.trueName'
			}),new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.agent.phone"/>',
				id:'tel',
				width : 220,
				name : 'agent.contactInfo.tel'
			}),new Ext.form.TextField({
				fieldLabel : '<fmt:message key="format.agent.email"/>',
				id:'email',
				width : 220,
				name : 'agent.contactInfo.email'
			})]
		});
	}

	
	function addNew() {
		addAgentWin = new Ext.Window({
			width : 620,
			height : 340,
			border : false,
			closable : false,
			layout : 'fit',
			items : addAgentForm,
			tbar : [{
				text : '<fmt:message key="format.message.confirm"/>',
				handler : create,
				formBind : true
			}, {
				text : '<fmt:message key="format.message.cancel"/>',
				handler : function() {
					addAgentWin.hide();
				}
			}],
			modal : true
		});

		addAgentWin.title = '<fmt:message key="format.agent.addAgent"/>';
		addAgentWin.show(Ext.getBody());
	}

	//新增方法
	create = function(btn) {
		var addatention = Ext.getCmp('addatention');
		if(addatention.getForm().isValid()){
			btn.disable();
			addatention.getForm().submit({
				method : 'POST',
				waitMsg : '<fmt:message key="format.message.saving"/>',
				successProperty : false,
				success : function() {
					Ext.Msg.alert('<fmt:message key="format.message.success"/>', 
						'<fmt:message key="format.message.save.success"/>',function() {
						btn.enable();
						store_agented.reload();
					});
				},
				failure : function() {
					Ext.Msg.alert('<fmt:message key="format.message.failure"/>', 
					'<fmt:message key="format.message.failure"/>');
					btn.enable();
				}
			});
			}
	}

	//编辑方法
	editagentListed=function() {

		editAgentWin = new Ext.Window({
			width : 620,
			height : 340,
			closable : false,
			layout : 'fit',
			items : editAgentForm,
			tbar : [{
				text : '<fmt:message key="format.message.confirm"/>',
				handler: function(btn) {
					if(editAgentForm.getForm().isValid()){
						btn.disable();
						editAgentForm.getForm().submit({
							method : 'POST',
							waitMsg : '<fmt:message key="format.message.saving"/>',
							successProperty : false,
							success : function() {
								Ext.Msg.alert('<fmt:message key="format.message.success"/>',
									'<fmt:message key="format.message.update.success"/>',function() {
									btn.enable();
									store_agented.reload();
								});
							},
							failure : function() {
								Ext.Msg.alert('<fmt:message key="format.message.failure"/>', 
									'<fmt:message key="format.message.update.failure"/>');
								btn.enable();
							}
						});
						}
				},
				formBind:true

			}, {
				text : '<fmt:message key="format.message.cancel"/>',
				handler : function() {
					editAgentWin.hide();
				}
			}],
			modal : true
		});

		var rec = grid_agented.getSelections()[0];
		if (rec != null) {
			editAgentWin.title = '<fmt:message key="format.bettingcell.edit"/>';
			
			editAgentWin.show(Ext.getBody());
			editAgentForm.getForm().loadRecord(rec);
		} else {
			Ext.MessageBox.alert('<fmt:message key="format.content.Warning"/>', 
			'<fmt:message key="format.bettingcell.selectPlay"/>');
		}
	}

	delAgentList =function (){
		var dict = grid_agented.getSelections()[0];
		if (dict != null) {
			Ext.MessageBox.confirm('<fmt:message key="format.message.delete"/>', '<fmt:message key="format.content.isdelete"/>', doDelAgentList)
		} else {
			Ext.MessageBox.alert('<fmt:message key="format.message.prompt"/>', '<fmt:message key="format.message.selectData"/>');
		}
	}

	function doDelAgentList(d){
		if (d == 'yes') {
			var r = grid_agented.getSelectionModel().getSelected();
			var rec = grid_agented.getSelections()[0];
			Ext.MessageBox.wait('<fmt:message key="format.content.wait"/>');
			Ext.Ajax.request( {
				url : 'Agent_delete.action',
				params : {
					'agent.uno' : rec.data.uno
				},
				success : function(response) {
					grid_agented.getStore().remove(rec);
					Ext.MessageBox.hide();
				},
				failure : function() {
					Ext.Msg.show( {
						title : '<fmt:message key="format.content.errortitle"/>',
						msg : '<fmt:message key="format.content.errormsg"/>',
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.ERROR
					});
				}
			});

		}
	}
	agenListedLog=function (){
		var rec = grid_agented.getSelections()[0];
		var titleName = '<fmt:message key="format.member.bettingHistory" />';
		openPage('agenListedLog',titleName,'/newbingo/pages/agentMgr/operationedLog.jsp',rec.data.uno);
	}
	
	betSetting = function (){
		var rec = grid_agented.getSelections()[0];
		var titleName = '<fmt:message key="format.menu.GameSetting" />';
		openPage('betSetting',titleName,'/newbingo/pages/agentMgr/betSetting.jsp',rec.data.uno);
	}
	statusUpdate = function (status){
		var rec = grid_agented.getSelections()[0];
		Ext.MessageBox.wait('<fmt:message key="format.content.wait"/>');
		Ext.Ajax.request( {
			url : 'Agent_updateStatus.action',
			method : 'POST',
			params : {
				'agent.uno' : rec.data.uno,
				'agent.status':status
			},
			success : function(response) {
				grid_agented.getStore().reload();
				Ext.MessageBox.hide();
			},
			failure : function() {
				Ext.Msg.show( {
					title : '<fmt:message key="format.content.errortitle"/>',
					msg : '<fmt:message key="format.content.errormsg"/>',
					buttons : Ext.Msg.OK,
					icon : Ext.Msg.ERROR
				});
			}
		});
		
	}
	
	//页面初始化
	return {
		init : function() {
			createUI();
		}
	};
}();
Ext.onReady(azure.common.agentListed.init, azure.common.agentListed, true);
</script>
</head>

	<body>
		<div id="agentedDiv"></div>
	</body>
</html>
